﻿using ConsoleApp1.Models.DB;
using ConsoleApp1.Uitls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.Services
{
    public class DataUploadRService : IDataUploadService<TResDetectionII>
    {



        private PrintService printService;

        private TResDetectionII lastData;

        private FreeSqlService freeSqlService;

        private MyHttpHelper myHttpHelper = new MyHttpHelper("http://192.168.1.11:8581/api/v1");


        public DataUploadRService(PrintService printService, FreeSqlService freeSqlService)
        {
            this.printService = printService;
            this.freeSqlService = freeSqlService;
            this.printService.SetTitle("接地阻抗");
        }

        public void Init()
        {
            printService.Debug("读取");
            lastData = freeSqlService.From
                .Select<TResDetectionII>()
                .OrderByDescending(t => t.Id)
                .First();

            if (lastData == null)
            {
                printService.Error("数据库为空");
            }
            else
            {
                printService.Debug($"最新的Id为{lastData.Id}");
                printService.Debug("发送最新一条数据用于测试");
                Send(lastData);
            }

        }

        public void LoopSend()
        {
            var lists = freeSqlService.From.Select<TResDetectionII>()
                .Where(t => t.Id > lastData.Id)
                .OrderByDescending(t => t.Id)
                .ToList();
            printService.Debug($"查询到{lists.Count}个新增数据");

            foreach (var item in lists)
            {
                Send(item);
            }
            lastData = freeSqlService.From
                .Select<TResDetectionII>()
                .OrderByDescending(t => t.Id)
                .First();
            printService.Info($"最新Id为{lastData.Id}");
        }

        public void Send(TResDetectionII data)
        {
            var api = "uploadJDZKValue";
            printService.Debug($"{data.TerminalId}发送到泰岳{api}");

            var sendData = new Rootobject()
            {
                terminalId = data.TerminalId,
                RAB = data.RAB.ToString(),
                RAC = data.RAC.ToString(),
                RBC = data.RBC.ToString(),
                VA = data.VA.ToString(),
                VB = data.VB.ToString(),
                VC = data.VC.ToString()
            };

            printService.Debug(JsonConvert.SerializeObject(sendData));
            myHttpHelper.MyHttpPost(api, sendData, null);

        }


        public class Rootobject
        {
            public string terminalId { get; set; }
            public string VA { get; set; }
            public string VB { get; set; }
            public string VC { get; set; }

            public string RAB { get; set; }
            public string RAC { get; set; }
            public string RBC { get; set; }
        }

    }
}
